package cn.lxg.service.impl;

import cn.lxg.dao.IUserBookrackDao;
import cn.lxg.dao.IUserDao;
import cn.lxg.pojo.TUser;
import cn.lxg.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private final IUserDao userDao = null;

    @Autowired
    private final IUserBookrackDao userBookrackDao = null;

    @Override
    @Transactional
    public String login(TUser user) {
        // 获得库中对应用户对象
        TUser oldUser = this.userDao.findByAccount(user.getAccount());
        if (oldUser == null || oldUser.getId() == 0)
            return "登陆失败，该账号不存在";

        // 校验密码是否正确
        if (!oldUser.getPassword().equals(user.getPassword()))
            return "登陆失败，密码错误";

        // 刷新最后登录时间
        oldUser.setEndLoginTime(new Date());
        this.userDao.updateById(oldUser);

        // 账号密码都校验正确
        user.setId(oldUser.getId());
        user.setName(oldUser.getName());
        user.setEndLoginTime(oldUser.getEndLoginTime());
        user.setCreateTime(oldUser.getCreateTime());

        return "OK";
    }

    @Override
    @Transactional
    public int countDataLine(TUser user) {
        return this.userDao.countDataLine(user);
    }

    @Override
    @Transactional
    public List<TUser> getList(Integer limit, Integer page, TUser user) {
        /* 计算页面 */
        Integer limitStart = null;
        Integer limitEnd = null;
        if (limit != null && page != null) {
            limitStart = (page - 1) * limit;
            limitEnd = limit;
        }
        return this.userDao.select(limitStart, limitEnd, user);
    }

    @Override
    @Transactional
    public TUser get(Integer id) {
        return this.userDao.findById(id);
    }

    @Override
    @Transactional
    public String add(TUser user) {
        // 检查账号是否重复
        TUser oldUser = this.userDao.findByAccount(user.getAccount());
        if (oldUser != null && oldUser.getId() != 0)
            return "添加用户失败，该账号已经存在";

        // 设置参数
        user.setEndLoginTime(new Date());
        user.setCreateTime(new Date());

        // 插入用户
        if (this.userDao.insert(user))
            return "添加用户成功";
        else
            return "添加用户失败";
    }

    @Override
    @Transactional
    public String delete(Integer id) {
        // 删除用户收藏
        this.userBookrackDao.deleteByUserId(id);

        // 删除用户
        if (this.userDao.deleteById(id))
            return "删除用户成功";
        else
            return "删除用户失败";
    }

    @Override
    @Transactional
    public String edit(TUser user) {
        // 判断账号是否重复
        TUser oldUser = this.userDao.findByAccount(user.getAccount());
        if (oldUser != null && oldUser.getId() != user.getId())
            return "该账号已经存在，编辑用户失败";

        // 查旧用户
        oldUser = this.userDao.findById(user.getId());

        // 更新属性
        oldUser.setName(user.getName());
        oldUser.setAccount(user.getAccount());
        oldUser.setPassword(user.getPassword());

        // 保存更改
        if (this.userDao.updateById(oldUser))
            return "编辑用户成功";
        else
            return "编辑用户失败";
    }
}
